package com.water.app.Impl.Goods;

import com.water.app.*;
import com.water.app.Exceptions.DeleteException;
import com.water.app.Exceptions.isPassException;
import com.water.app.interfaces.Goods.shopService;
import com.water.app.mapper.goodsMapper;
import com.water.app.mapper.shopMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class shopServiceImpl implements shopService {

    @Resource
    private shopMapper shopMapper;

    @Resource
    private goodsMapper goodsMapper;
    @Override
    public shopParam selectShopParamById(int shopId) throws isPassException {
        shopParam shopParam=new shopParam();
        shop shop = shopMapper.selectShopById(shopId);
        shopParam.setShop(shop);
        if(shop.getIsPass()==0){
            throw new isPassException("店铺还在审核中，请等待消息");
        }else if(shop.getIsPass()==2){
            throw new isPassException("店铺已被封禁，详情请联系管理员");
        }
        List<Goods> list=goodsMapper.selectAllByShopId(shopId);
        shopParam.setGoodsList(list);
        List<shopGoodsType> shopGoodsTypeList= shopMapper.selectShopTypeByShopId(shopId);
        shopParam.setShopGoodsTypeList(shopGoodsTypeList);
        List<Type> typeList=goodsMapper.selectType();
        shopParam.setTypeList(typeList);
        Integer userId = Constant.THREAD_LOCAL.get();
        Integer userId2= goodsMapper.selectIsMyShop(shopId);
        if(Objects.equals(userId2, userId)){
            shopParam.setIsMyShop(1);
        }else {
            shopParam.setIsMyShop(0);
        }
        return shopParam;
    }

    @Override
    public boolean insertShop(shop shop,Integer userId) {
        return shopMapper.insertShop(shop,userId)>0;
    }

    @Override
    public boolean updatePicture(String url,Integer shopId) {
        return shopMapper.updatePicture(url,shopId)>0;
    }

    @Override
    public List<? extends shop> selectShopList(Integer userId) {
        return shopMapper.selectShopList(userId);
    }

    @Override
    public boolean deleteShopById(Integer shopId) throws DeleteException {
        Integer userId = Constant.THREAD_LOCAL.get();

        try {
            return shopMapper.deleteShopById(shopId,userId)>0;
        } catch (Exception e) {
            throw new DeleteException("这不是您的店铺，没有权限删除",e);
        }
    }
}
